<template>
  <div class="app-container">

    <el-dialog title="新增章节" :visible.sync="novel" width="1200px" append-to-body>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['novel:novel:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['novel:novel:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['novel:novel:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['novel:novel:export']"
          >导出</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="el-icon-sort"
            size="mini"
            @click="toggleExpandAll"
          >展开/折叠</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading"
                :data="novelList"
                @selection-change="handleSelectionChange"
                row-key="id"
                :default-expand-all="isExpandAll"
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column type="selection" width="55" align="center" />
        <!--      <el-table-column label="分类一" align="center" prop="type1" />-->
        <el-table-column label="标题" align="center" prop="title" width="200" />
        <el-table-column label="分类" align="center" prop="type2Name" />
        <el-table-column label="排序" align="center" prop="sort" />
        <el-table-column label="图片" align="center" prop="image" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.image" :width="50" :height="50"/>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="state">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.novel_novel_state" :value="scope.row.state"/>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="300" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['novel:novel:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['novel:novel:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-dialog>

    <!-- 添加或修改小说漫画对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="分类一" prop="type1">-->
<!--          <el-input v-model="form.type1" placeholder="请输入分类一" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="分类" prop="type2">-->
<!--          <treeselect v-model="form.type2" :options="typeOptions" :normalizer="normalizer" placeholder="请选择父分类" @input="bindType"/>-->
<!--        </el-form-item>-->
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="图片" prop="image">
          <image-upload v-model="form.image" :limit="1"/>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <editor v-model="form.content" :min-height="192"/>
<!--          <image-upload v-if="$route.query.mode==1" v-model="form.content"/>-->
        </el-form-item>
        <el-form-item label="会员等级" prop="integral">
          <el-select v-model="form.integral" placeholder="请选择会员等级">
            <el-option
              v-for="dict in dict.type.novel_vip_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.novel_novel_state"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNovel, getNovel, delNovel, addNovel, updateNovel } from "@/api/novel/novel";
import { listType } from '@/api/novel/type'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Novel",
  dicts: ['novel_novel_state','novel_vip_type'],
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小说漫画表格数据
      novelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pId: null,
        type2: null,
        title: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        image: [
          { required: true, message: "图片不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
      },
      typeList: [],
      typeOptions: [],
      novel: null,
    };
  },
  created() {

  },
  methods: {
    bindType: function(e) {
      let arr = this.typeList.filter(item => item.id == e)
      console.log(arr)
      if(arr.length > 0){
        this.form.type1 = arr[0].pId;
      }
    },
    /** 转换分类信息数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
    /** 查询分类信息下拉树结构 */
    getTypeTreeselect() {
      listType({
        mode: this.queryParams.mode+2
      }).then(response => {
        this.typeList = response.data;
        this.typeOptions = [];
        const data = { id: 0, name: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "id", "pId");
        this.typeOptions.push(data);
      });
    },
    getNovelList(row){
      this.novel = row;
      this.queryParams.pId = this.novel.id;
      this.queryParams.mode = this.novel.mode;
      this.getList();
    },
    /** 查询小说漫画列表 */
    getList() {
      this.loading = true;
      listNovel(this.queryParams).then(response => {
        this.novelList = this.handleTree(response.rows, "id", "pId");;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type1: null,
        type2: null,
        sort: null,
        title: null,
        image: null,
        content: null,
        integral: null,
        mode: null,
        state: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
      this.queryParams.mode = this.$route.query.mode;
      this.queryParams.pId = this.novel.id;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTypeTreeselect();
      this.open = true;
      this.title = "添加小说漫画";
      if(row){
        this.form.pId = this.novel.id;
        this.form.type1 = this.novel.type1;
        this.form.type2 = this.novel.type2;
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTypeTreeselect();
      const id = row.id || this.ids
      getNovel(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改小说漫画";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.pId = this.novel.id;
          this.form.mode = this.$route.query.mode;
          if (this.form.id != null) {
            updateNovel(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNovel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除小说漫画编号为"' + ids + '"的数据项？').then(function() {
        return delNovel(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('novel/novel/export', {
        ...this.queryParams
      }, `novel_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
